package com.weichat.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.User;
import com.service.UserService;
import com.weichat.tool.WeiChatUtil;

@Controller
public class BusLocationController {
	@Autowired
	UserService userService;

	@RequestMapping("checkCar")
	public String location(HttpServletRequest req) {
		String html = "";
		String code = req.getParameter("code");
		String openId;
		if (code != null) {
			openId = WeiChatUtil.getOpenId(code);
			req.getSession().setAttribute("openId", openId);
			System.out.println("code==" + code + " ---openId==" + openId);
		} else {
			openId = (String) req.getSession().getAttribute("openId");
		}
		Calendar now = Calendar.getInstance();
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss
		// EEEE");
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE", Locale.ENGLISH);
		String dateNowStr = sdf.format(now.getTimeInMillis());
		SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm");
		String dateNowStr1 = sdf1.format(now.getTimeInMillis());
		int hour = Integer.parseInt(dateNowStr1.substring(0, dateNowStr1.indexOf(":")));
		int min = Integer.parseInt(dateNowStr1.substring(dateNowStr1.indexOf(":") + 1, dateNowStr1.length()));
		System.out.println("小时===" + hour + "分钟====" + min);
		if (userService.selectByOpenId(openId).size() == 0) {
			html = "register.html";
		} else if (dateNowStr.equals("Monday") || dateNowStr.equals("Wednesday") || dateNowStr.equals("Friday")
				|| dateNowStr.equals("Saturday") || dateNowStr.equals("Sunday") || dateNowStr.equals("Thursday")) {
			// if ((hour >= 7 && min >= 30) && (hour <= 8 && min < 32)) {
			User u = userService.selectByOpenId(openId).get(0);
			req.getSession().setAttribute("user", u);
			System.out.println("查看营车位置按钮");
			html = "SeeSchoolBus.html";
			/*
			 * }else{ System.out.println("此时不能查看营车位置按钮"); html="Empty.html"; }
			 */
		} else if (dateNowStr.equals("Tuesday")) {
			html = "free.html";
		}

		System.out.println("html======" + html);
		// html="sports.html";
		return html;
	}

}
